#!/bin/sh
#fastdfs自动化安装脚本
#服务器IP
tracker_server_ip='192.168.118.3'
#fastdfs跟踪器路径
tracker_path='/home/fdfs/tracker'
#fastdfs存储节点路径
stroage_path='/home/fdfs/stroage'

echo "----- fastdfs自动化安装脚本开始执行 -----"
#安装gcc
echo "----- 安装gcc -----"
cd ../rpm/gcc
rpm  -ivh  *.rpm --nodeps --force
gcc -v
#初始化目录
echo "----- 初始化目录 -----"
if [ ! -d ${tracker_path} ];then
  mkdir -p ${tracker_path};
fi
if [ ! -d ${stroage_path} ];then
  mkdir -p ${stroage_path};
fi
#安装libfastcommon
echo "----- 安装libfastcommon -----"
tar -zxvf ../fastdfs/libfastcommon-1.0.7.tar.gz -C ../../temp
cd ../../temp/libfastcommon-1.0.7
./make.sh && ./make.sh install
#安装fastdfs
echo "----- 安装fastdfs -----"
tar -zxvf ../../rpm/fastdfs/fastdfs-5.05.tar.gz -C ../../temp
cd ../../temp/fastdfs-5.05
./make.sh && ./make.sh install
#修改配置文件
echo "----- 修改配置文件 -----"
cp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf
cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf
cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf
cp ./conf/mime.types  /etc/fdfs/
cp ./conf/http.conf  /etc/fdfs/
sed -i '10c base_path='$stroage_path'' /etc/fdfs/client.conf
sed -i '14c tracker_server='$tracker_server_ip':22122' /etc/fdfs/client.conf
sed -i '22c base_path='$tracker_path'' /etc/fdfs/tracker.conf
sed -i '45c store_group=group1' /etc/fdfs/tracker.conf
sed -i '41c base_path='$stroage_path'' /etc/fdfs/storage.conf
sed -i '109c store_path0='$stroage_path'' /etc/fdfs/storage.conf
sed -i '118c tracker_server='$tracker_server_ip':22122' /etc/fdfs/storage.conf
if [ ! -e /usr/bin/fdfs_trackerd ];then
  ln -s /usr/bin/fdfs_trackerd /usr/local/bin/
fi
if [ ! -e /usr/bin/fdfs_storaged ];then
  ln -s /usr/bin/fdfs_storaged /usr/local/bin/
fi
if [ ! -e /usr/bin/stop.sh ];then
  ln -s /usr/bin/stop.sh /usr/local/bin/
fi
if [ ! -e /usr/bin/restart.sh ];then
  ln -s /usr/bin/restart.sh /usr/local/bin/
fi
#配置开机自启动
echo "----- 配置开机自启动 -----"
chkconfig --add fdfs_trackerd
chkconfig fdfs_trackerd on
chkconfig --add fdfs_storaged
chkconfig fdfs_storaged on
#启动服务
echo "----- 启动服务 -----"
service fdfs_trackerd start
ss -lntup|grep 22122
service fdfs_storaged start
ss -lntup|grep 23000
#开放22122端口访问权限
echo "----- 开放22122端口访问权限 -----"
systemctl start firewalld
if [ $? -eq 0 ];then
   echo "----- 成功开启防火墙 -----"
else
   echo "----- 防火墙开启失败，脚本退出 -----"
   exit 1
fi
firewall-cmd --zone=public --add-port=22122/tcp --permanent
if [ $? -eq 0 ];then
   firewall-cmd --reload
   echo "----- 22122端口开启成功 -----"
else
   echo "----- 22122端口开启失败，脚本退出 -----"
   exit 1
fi
#安装fastdfs-nginx-module
echo "----- 安装fastdfs-nginx-module -----"
tar -zxvf ../../rpm/fastdfs/fastdfs-nginx-module_v1.16.tar.gz -C ../../temp
cd ../fastdfs-nginx-module
sed -i "s:/usr/local:/usr:g" ./src/config
cp ./src/mod_fastdfs.conf /etc/fdfs/
sed -i '40c tracker_server='$tracker_server_ip':22122' /etc/fdfs/mod_fastdfs.conf
sed -i '53c url_have_group_name=true' /etc/fdfs/mod_fastdfs.conf
sed -i '62c store_path0='$stroage_path'' /etc/fdfs/mod_fastdfs.conf
echo "----- fastdfs自动化安装脚本执行完毕 -----"
